package com.example.cinema.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.example.cinema.entity.BizExeption;
import com.example.cinema.entity.Hall;
import com.example.cinema.entity.Studios;
import com.example.cinema.entity.service.impl.HallServiceImpl;
import com.example.cinema.mapper.HallMapper;
import com.example.cinema.mapper.StudiosMapper;
import com.example.cinema.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yc138
 * @since 2024-08-26
 */
@RestController
@RequestMapping("/hall")
public class HallController {
    @Autowired
    HallMapper hallMapper;
    @Autowired
    StudiosMapper studiosMapper;
    @RequestMapping("query")
    public Page<Hall> queryStudios(@RequestBody
                                      @RequestParam(defaultValue = "1") int current,
                                      @RequestParam(defaultValue = "5")int num,String type,String stuId){
        LambdaQueryWrapper<Hall> lqw=new LambdaQueryWrapper<>();
        if(!"请选择影院".equals(stuId)){
            lqw.eq(Hall::getStuId,stuId);
        }
        if(type!=null&&type.isBlank()==false){
            lqw.like(Hall::getType,type);
        }
        Page<Hall> p = new PageDTO<>(current, num);
        Page<Hall> hallPage = hallMapper.selectPage(p, lqw);
        for (int i = 0; i < hallPage.getRecords().size(); i++) {
           hallPage.getRecords().get(i).setStudios(studiosMapper.selectById( hallPage.getRecords().get(i).getStuId()));
        }
        return hallPage;
    }
    //删除hall
    @RequestMapping("del")
    Result del(@RequestParam int id){
        int i = hallMapper.deleteById(id);
        if(i>0){
            return new Result(1,"删除成功",null);
        }else{
            return new Result(-1,"删除失败",null);
        }
    }
    //修改
    @RequestMapping("change")
    Result change(@RequestBody Hall hall){
       hallMapper.deleteById(hall.getId());
        int insert = hallMapper.insert(hall);
        if (insert>0){
            return new Result(1,"修改成功",null);
        }else{
            return new Result(-1,"修改失败",null);
        }
    }
    //添加
    @Autowired
    HallServiceImpl hallService;
    @RequestMapping("add")
    Result add(@Valid @RequestBody Hall hall, Errors errors){
        if(hall.getStuId()==null){
            errors.rejectValue("stuId", "is_empty", "影院名不能为空!");
        }
        if(hall.getNum()==null){
            errors.rejectValue("num", "is_empty", "数量不能为空");
        }
        if (errors.hasErrors()) {
            return new Result(0, "添加影厅信息失败!", errors.getAllErrors());
        }
        try {
            hallService.add(hall);
            return new Result(1, "添加影厅成功!", null);
        } catch (BizExeption e) {
            return new Result(0, "添加影厅失败!", null);
        }
    }
    @RequestMapping("selStudios")
    public List<Studios> selStudios(){
        LambdaQueryWrapper<Studios> lqw=new LambdaQueryWrapper<>();
        List<Studios> studios = studiosMapper.selectList(null);
       return studios;
    }
}

